package com.amt.olcacon.utils;

import lombok.extern.slf4j.Slf4j;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class RegexUrlConverter {
    
    public static String convertUrlToFilename(String url) {
        // 使用正则表达式提取所有参数
        Pattern pattern = Pattern.compile("fileName=([^&]*)&fut=([^&]*)&ai=([^&]*)&con=([^&]*)");
        Matcher matcher = pattern.matcher(url);
        log.info("URL: {}", url);
        
        if (matcher.find()) {
            String fileName = matcher.group(1).replace(".xlsx", "");
            String fut = matcher.group(2);
            String ai = matcher.group(3);
            String con = matcher.group(4);
            return String.format("%s_fut%s_ai%s_con%s.xlsx",
                               fileName, fut, ai, con);
        }
        
        throw new IllegalArgumentException("URL格式不符合预期模式");
    }

    public static String convertUrlToFilenameWord(String url) {
        // 使用正则表达式提取所有参数
        Pattern pattern = Pattern.compile("fileName=([^&]*)&fut=([^&]*)&ai=([^&]*)&con=([^&]*)");
        Matcher matcher = pattern.matcher(url);
        log.info("URL: {}", url);

        if (matcher.find()) {
            String fileName = matcher.group(1).replace(".docx", "");
            String fut = matcher.group(2);
            String ai = matcher.group(3);
            String con = matcher.group(4);
            return String.format("%s_fut%s_ai%s_con%s.docx",
                    fileName, fut, ai, con);
        }

        throw new IllegalArgumentException("URL格式不符合预期模式");
    }


    public static void main(String[] args) {
        String url = "/upload/5MWH模组导入测试-01(3).xlsx?fileName=5MWH模组导入测试-01(3).xlsx&fut=1761285866932&ai=be6816b9-777c-4ada-8e3c-2ff20ca75f10&con=lcap_default_connection&fpp=productcarbon";

        String result = convertUrlToFilename(url);
        System.out.println("转换结果: " + result);
    }
//    /upload/5MWH模组导入测试.xlsx?fileName=5MWH模组导入测试.xlsx&fut=1761193153599&ai=be6816b9-777c-4ada-8e3c-2ff20ca75f10&con=lcap_default_connection&fpp=app
//    ///upload/5MWH模组导入测试.xlsx?fileName=5MWH模组导入测试.xlsx&fut=1761192679659&ai=be6816b9-777c-4ada-8e3c-2ff20ca75f10&con=lcap_default_connection&fpp=app
}